#!/usr/bin/perl
use warnings;
use strict;
use CGI;
require 'doctorIO.pl';
system("./pdf.pl");
print "Content-type: text/html\n\n";
print <<PDF;
<a href="sched.pdf">View Today's Schedule</a><br><br>
<a href="loginlog.txt">View Login Attempts</a><br><br>
PDF
#if there is stuff in POST
if ($ENV{'CONTENT_LENGTH'}.length() > 0){
	my $readin;
	read(STDIN, $readin, $ENV{'CONTENT_LENGTH'});
	my $inputstring = $readin;
	my @pairsarr = split(/&/, $inputstring);
	my $schar = substr($inputstring, 0, 1);
	#if we want to delete a doctor
	if ($schar eq "d"){
		my @del = split /=/,$pairsarr[0];
		$del[1] =~ s/\+/ /g;
		system("./edit_doctors.pl -dd '$del[1]'");
	}
	#if we want to add a doctor
	else {
		my $text = "";
		foreach my $pair (@pairsarr){
			my @sp = split /=/,$pair;
			$sp[1] =~ s/\+/ /g;
			$text .= "'$sp[1]' ";
		}
		system("./edit_doctors.pl -ad $text");
	}
	#print out success/fail message
	open(FH, "log.txt");
	print <FH>;
	close(FH);
}
print <<FORMHEAD;
<form name="form1" action="admin.cgi" method=POST>
<table border=1>
<tr><td>Name:<td><input type="text" name="name"></tr>
<tr><td>Type:<td><input type="text" name="type"></tr>
<tr><td>Start:<td>
    <select name="start">
        <option value="9">9AM</option>
        <option value="10">10AM</option>
        <option value="11">11AM</option>
        <option value="12">12PM</option>
        <option value="13">1PM</option>
        <option value="14">2PM</option>
        <option value="15">3PM</option>
        <option value="16">4PM</option>
    </select>
</tr>
<tr><td>End:<td>
    <select name="end">
        <option value="10">10AM</option>
        <option value="11">11AM</option>
        <option value="12">12PM</option>
        <option value="13">1PM</option>
        <option value="14">2PM</option>
        <option value="15">3PM</option>
        <option value="16">4PM</option>
        <option value="17">5PM</option>
    </select>
</tr>
<tr><td><td>
<input type="submit" value="Add Doctor">
</table>
</form>
<form name="form2" action="admin.cgi" method=POST>
<select name="deldoc">
FORMHEAD
foreach my $doctor (get_doctors()){
	print "<option value=\"$doctor\">$doctor</option>\n";
}
print <<FORMEND;
</select>
<input type="submit" value="Delete Doctor">
</form>
FORMEND
